﻿namespace ZeroSum.Extensions;

/// <summary>
///     Enumerable集合扩展方法
/// </summary>
public static class EnumerableExtensions
{
    /// <summary>
    ///     将字符串集合按指定前缀排序
    /// </summary>
    public static IEnumerable<T> OrderByPrefixes<T>(this IEnumerable<T> source, Func<T, string> keySelector,
        params string[] prefixes)
    {
        var all = source.OrderBy(keySelector).ToList();
        var result = new List<T>();
        foreach (var prefix in prefixes)
        {
            var tmpList = all.Where(m => keySelector(m).StartsWith(prefix)).OrderBy(keySelector).ToList();
            all = all.Except(tmpList).ToList();
            result.AddRange(tmpList);
        }

        result.AddRange(all);
        return result;
    }
}